import java.util.StringTokenizer;


class P11340_39643 {

	public static void main(String[] args) {
		int cases = Integer.parseInt(readLn());
		
		for (int k = 0; k < cases; k++) {
			int letters = Integer.parseInt(readLn());
			
			long[] values = new long[256];
			
			
			for (int j = 0; j < letters; j++) {
				StringTokenizer st = new StringTokenizer(readLn());
				char c = st.nextToken().charAt(0);
				long value = Long.parseLong(st.nextToken());
				values[c] = value;
			}

			int lines = Integer.parseInt(readLn());
			
			long result = 0;
			
			for (int j = 0; j < lines; j++) {
				
				String line = readLn();
				
				for (int i = 0; i < line.length(); i++) {
					result += values[line.charAt(i)];
				}
				
			}
			
			String s = Long.toString(result);

			String resultformatted = "";
			
			if (s.length() == 1) {
				resultformatted = "0.0" + s;
			} else if (s.length() == 2) {
				resultformatted = "0." + s;
			} else {
			
				int counter = 0;
				for (int j = s.length() - 1; j >= 0; j--) {
					resultformatted = s.charAt(j) + resultformatted;
					if (counter == 1) {
						resultformatted = "." + resultformatted;
					}
					counter++;
				}
			
			}
			
			System.out.println(resultformatted + "$");
//			System.out.println((result / 100) + "." + ((result % 100 < 10) ? (result / 100 + "0") : (result % 100)) + "$");
			
		}
		
	}
	
    static int readLn(int[] values) {
        int value = 0;
    	String newLine = System.getProperty("line.separator");
//        StringBuilder buffer = new StringBuilder();
        int car = -1;
        try {
            car = System.in.read();
            while ((car > 0) && (car != newLine.charAt(0))) {
            	value += values[car];
            	//                buffer.append((char) car);
                car = System.in.read();
            }
            if (car == newLine.charAt(0))
                System.in.skip(newLine.length() - 1);
        } catch (java.io.IOException e) {
            return 0;
        }
        if ((car < 0)) //&& (buffer.length() == 0))
            return 0;
        return value;
    }
	 
    static String readLn() {
        String newLine = System.getProperty("line.separator");
        StringBuilder buffer = new StringBuilder();
        int car = -1;
        try {
            car = System.in.read();
            while ((car > 0) && (car != newLine.charAt(0))) {
                buffer.append((char) car);
                car = System.in.read();
            }
            if (car == newLine.charAt(0))
                System.in.skip(newLine.length() - 1);
        } catch (java.io.IOException e) {
            return (null);
        }
        if ((car < 0) && (buffer.length() == 0))
            return (null);
        return (buffer.toString()).trim();
    }
	
}
